package org.advanced.aqs;

import java.util.concurrent.CountDownLatch;

/**
 * @author John.zhang
 * @version Id: CountDownLatches, v 1.0 2020/12/10 15:49 ZhiYong Exp $
 * 需要所有的线程都执行完毕才会结束，核心是通过CountDownLatch--为0 通过传递的方式唤醒所有线程
 */
public class CountDownLatches {

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(3);

        //倒计时3-1
        new Thread(() ->
                //修改state = state-1通过cas[修改某个值的偏移量设置]设置到这个字段
                countDownLatch.countDown()
        ).start();

        //倒计时2-1
        new Thread(() -> countDownLatch.countDown()).start();

        //倒计时1-1
        new Thread(() -> countDownLatch.countDown()).start();

        //线程阻塞
        countDownLatch.wait();
        System.out.println("线程执行完毕");
    }


}
